package com.gxc.healthfood.common;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 统一响应结果类，用于API接口返回标准化的数据结构
 */
public class ResponseResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    
    // 响应状态：true表示成功，false表示失败
    private boolean success;
    
    // 响应消息
    private String message;
    
    // 响应数据
    private T data;
    
    // 无参构造函数
    public ResponseResult() {
    }
    
    // 有参构造函数
    public ResponseResult(boolean success, String message, T data) {
        this.success = success;
        this.message = message;
        this.data = data;
    }
    
    // Getter和Setter方法
    public boolean isSuccess() {
        return success;
    }
    
    public ResponseResult<T> setSuccess(boolean success) {
        this.success = success;
        return this;
    }
    
    public String getMessage() {
        return message;
    }
    
    public ResponseResult<T> setMessage(String message) {
        this.message = message;
        return this;
    }
    
    public T getData() {
        return data;
    }
    
    public ResponseResult<T> setData(T data) {
        this.data = data;
        return this;
    }
    
    // 静态工厂方法：创建成功响应
    public static <T> ResponseResult<T> success(T data) {
        return new ResponseResult<>(true, "操作成功", data);
    }
    
    public static <T> ResponseResult<T> success(String message, T data) {
        return new ResponseResult<>(true, message, data);
    }
    
    // 静态工厂方法：创建失败响应
    public static <T> ResponseResult<T> error(String message) {
        return new ResponseResult<>(false, message, null);
    }
    
    public static <T> ResponseResult<T> error(String message, T data) {
        return new ResponseResult<>(false, message, data);
    }
    
    // 转换为Map格式（可选，便于某些场景使用）
    public Map<String, Object> toMap() {
        Map<String, Object> map = new HashMap<>();
        map.put("success", success);
        map.put("message", message);
        map.put("data", data);
        return map;
    }
}